package date_demo;

/**
 * @author ChenYongHao
 * @date 2023/5/4 10:15
 * @desc 描述：用于计算两个“时间”间隔，可以获取天、时、时、分、秒、毫秒、纳秒
 */

import java.time.Duration;
import java.time.Instant;

public class Duration_Demo {
    public static void main(String[] args) {
        Instant ins1 = Instant.now();

        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        Instant ins2 = Instant.now();

        Duration duration = Duration.between(ins1, ins2);
        long days = duration.toDays();// 天
        long hours = duration.toHours();// 时
        long minutes = duration.toMinutes();// 分
        long seconds = duration.getSeconds();// 秒
        long millis = duration.toMillis();// 毫秒
        long nanos = duration.toNanos();// 纳秒

        System.out.println("“时间”间隔----天"+days);
        System.out.println("“时间”间隔----小时"+hours);
        System.out.println("“时间”间隔----分钟"+minutes);
        System.out.println("“时间”间隔----秒"+seconds);
        System.out.println("“时间”间隔----毫秒"+millis);
        System.out.println("“时间”间隔----纳秒"+nanos);
    }
}
